<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseTitle">
        <el-input
          v-model="queryParams.courseTitle"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择科目" prop="eduSubjectId">
        <el-select v-model="queryParams.eduSubjectId" placeholder="请选择科目类别" clearable>
          <el-option
            v-for="dict in subjectList"
            :key="dict.id"
            :label="dict.subjectName"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程类型" prop="courseType">
        <el-select v-model="queryParams.courseType" placeholder="请选择课程类型" clearable>
          <el-option
            v-for="dict in dict.type.course_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级类型" prop="classType">
        <el-select v-model="queryParams.classType" placeholder="请选择班级类型" clearable>
          <el-option
            v-for="dict in dict.type.course_class_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程总价" prop="coursePrice">
        <el-input
          v-model="queryParams.coursePrice"
          placeholder="请输入课程总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程次数" prop="courseNumber">
        <el-input
          v-model="queryParams.courseNumber"
          placeholder="请输入课程次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="截止日期" prop="deadlineDate">
        <el-date-picker clearable
          v-model="queryParams.deadlineDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="课时费用" prop="teacherFee">
        <el-input
          v-model="queryParams.teacherFee"
          placeholder="请输入课时费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['educational:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['educational:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['educational:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['educational:course:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" size="mini" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-descriptions :title="'['+props.row.courseTitle+']:预约详情'" style="margin-left: 50px;" class="margin-top" :column="3" size="mini" border>
            <el-descriptions-item>
              <template slot="label">开放预约</template> <dict-tag :options="dict.type.course_open_order" :value="props.row.openOrder"/>
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">是否上架</template> <dict-tag :options="dict.type.course_whether_shelf" :value="props.row.whetherShelf"/>
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">精品推荐</template> <dict-tag :options="dict.type.course_whether_recommend" :value="props.row.whetherRecommend"/>
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">课程库存</template> {{props.row.courseInventory}}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label">停售日期</template> {{ parseTime(props.row.stopDate, '{y}-{m}-{d}') }}
            </el-descriptions-item>
            <el-descriptions-item contentStyle="width: 450px;">
              <template slot="label">课程描述</template> {{ props.row.remark }}
            </el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程名称" align="center" prop="courseTitle"/>
      <el-table-column label="科目" align="center" prop="eduSubject.subjectName" />
      <el-table-column label="课程类型" align="center" prop="courseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_type" :value="scope.row.courseType"/>
        </template>
      </el-table-column>
      <el-table-column label="班级类型" align="center" prop="courseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.course_class_type" :value="scope.row.classType"/>
        </template>
      </el-table-column>
      <el-table-column label="课程封面" align="center" prop="courseCover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.courseCover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="课程总价" align="center" prop="coursePrice">
        <template slot-scope="scope">
          <span>{{ scope.row.coursePrice }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="课程次数" align="center" prop="courseNumber">
        <template slot-scope="scope">
          <span>{{ scope.row.courseNumber }}节课</span>
        </template>
      </el-table-column>
      <el-table-column label="教师课费" align="center" prop="teacherFee">
        <template slot-scope="scope">
          <span>{{ scope.row.teacherFee }}元/课</span>
        </template>
      </el-table-column>
      <el-table-column label="截止日期" align="center" prop="deadlineDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['educational:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['educational:course:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教务课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="课程名称" prop="courseTitle">
              <el-input v-model="form.courseTitle" placeholder="请输入课程名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="绑定科目" prop="eduSubjectId">
                <el-select v-model="form.eduSubjectId" placeholder="请选择绑定科目" style="width:100%">
                  <el-option
                    v-for="dict in subjectList"
                    :key="dict.id"
                    :label="dict.subjectName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="班级类型" prop="classType">
                <el-select v-model="form.classType" placeholder="请选择班级类型">
                  <el-option
                    v-for="dict in dict.type.course_class_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="8">
              <el-form-item label="课程总价" prop="coursePrice">
                <el-input v-model="form.coursePrice" placeholder="请输入课程总价" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="课程次数" prop="courseNumber">
                <el-input v-model="form.courseNumber" placeholder="请输入课程次数" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="课程类型" prop="courseType">
                <el-select v-model="form.courseType" placeholder="请选择课程类型" clearable>
                  <el-option
                    v-for="dict in dict.type.course_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="8">
              <el-form-item label="课时费用" prop="teacherFee">
                <el-input v-model="form.teacherFee" placeholder="请输入课时费用" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="开放预约" prop="openOrder">
                <el-select v-model="form.openOrder" placeholder="请选择开放预约" style="width:100%">
                  <el-option
                    v-for="dict in dict.type.course_open_order"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="截止日期" prop="deadlineDate" style="width:100%">
                <el-date-picker clearable
                  v-model="form.deadlineDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择截止日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
        </el-row>
        <el-divider content-position="center">在线购课</el-divider>
        <el-row>
            <el-col :span="8">
              <el-form-item label="是否上架" prop="whetherShelf">
                <el-select v-model="form.whetherShelf" placeholder="请选择是否上架" style="width:100%">
                  <el-option
                    v-for="dict in dict.type.course_whether_shelf"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否推荐" prop="whetherRecommend">
                <el-select v-model="form.whetherRecommend" placeholder="请选择是否推荐" style="width:100%">
                  <el-option
                    v-for="dict in dict.type.course_whether_recommend"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="课程库存" prop="courseInventory">
                <el-input v-model="form.courseInventory" placeholder="请输入课程库存" />
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="8">
              <el-form-item label="停售日期" prop="stopDate">
                <el-date-picker clearable
                  v-model="form.stopDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请选择停售日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
              <el-form-item label="课程封面" prop="courseCover">
                <image-upload :limit="1" v-model="form.courseCover"/>
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { allSubject } from "@/api/educational/subject";
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/educational/course";

export default {
  name: "Course",
  dicts: ['course_type', 'course_class_type', 'course_open_order', 'course_whether_shelf', 'course_whether_recommend'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教务课程表格数据
      courseList: [],
      // 教务科目表格数据
      subjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseTitle: null,
        eduSubjectId: null,
        coursePrice: null,
        courseNumber: null,
        deadlineDate: null,
        classType: null,
        courseType: null,
        teacherFee: null,
        openOrder: null,
        whetherShelf: null,
        whetherRecommend: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseTitle: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ],
        courseCover: [
          { required: true, message: "课程封面不能为空", trigger: "blur" }
        ],
        eduSubjectId: [
          { required: true, message: "科目外键不能为空", trigger: "change" }
        ],
        classType: [
          { required: true, message: "班级类型不能为空", trigger: "change" }
        ],
        courseType: [
          { required: true, message: "科目类型不能为空", trigger: "change" }
        ],
        coursePrice: [
          { required: true, message: "课程总价不能为空", trigger: "blur" }
        ],
        courseNumber: [
          { required: true, message: "课程次数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getSubjectList();
  },
  methods: {
    /** 查询教务课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getSubjectList() {
      allSubject().then(response => {
        this.subjectList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseTitle: null,
        courseCover: null,
        eduSubjectId: null,
        coursePrice: null,
        courseNumber: null,
        deadlineDate: null,
        courseType: null,
        teacherFee: null,
        openOrder: null,
        whetherShelf: null,
        whetherRecommend: null,
        courseInventory: null,
        stopDate: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教务课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教务课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除教务课程编号为"' + ids + '"的数据项？').then(function() {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('educational/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>